package me.jessyan.armscomponent.commonres.dialog;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;


import androidx.annotation.NonNull;
import me.jessyan.armscomponent.commonres.R;

/**
 * @Author: xuwei
 * @Date: 2020/12/22 9:03
 * @Description: 自定义通用加载弹窗
 */
public class CommonProgressDialog extends ProgressDialog {

    private TextView tvTitle;
    private CharSequence message;

    public CommonProgressDialog(@NonNull Context context) {
        super(context, R.style.public_custom_dialog);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
    }

    private void init() {
//        setCancelable(false);
        setCanceledOnTouchOutside(false);
        setContentView(R.layout.public_common_progress_dialog);
        tvTitle = findViewById(R.id.tv_title);
        Window window = getWindow();
        if (window != null) {
            WindowManager.LayoutParams params = window.getAttributes();
            params.width = WindowManager.LayoutParams.WRAP_CONTENT;
            params.height = WindowManager.LayoutParams.WRAP_CONTENT;
            getWindow().setAttributes(params);
        }
    }

    @Override
    public void show() {
        super.show();
        if (!TextUtils.isEmpty(message) || tvTitle != null) {
            tvTitle.setText(message);
        }
    }

    @Override
    public void setMessage(CharSequence message) {
        this.message = message;
    }
}
